// pages/mine/myLevel/myLevel.js
const app = getApp(),
    globalData = app.globalData,
    api = require("../../../api/index"),
    utils = require('../../../utils/util');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: 0,
        toast: {
            visible: false,
            text: ''
        },
        imageBaseUri: globalData.imageBaseUri,
        // active: 4
        userData: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload()
    },

    reload() {
        this.setData({
            userInfo: wx.getStorageSync('userinfo') || {},
        })

        console.log(this.data.userInfo, 'userInfo')
        if (this.data.userInfo.AccessToken) {
            this.getUser();
            this.getMySales();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    // 登录后获取用户等级信息
    async getUser() {
        try {
            let res = await api.user.getUser(),
                data = res.data.ResData || {};
            this.setData({
                agentInfo: data,
                status: 2
            })
            console.log(res, '获取用户等级信息')
        } catch (e) {
            console.log(e, '获取用户等级信息失败')
            this.setData({
                toast: {
                    visible: true,
                    text: e.data.ErrMsg
                },
                status: 1
            })
        }
    },

    // 获取本人的销售额
    async getMySales() {
        try {
            let res = await api.member.getSales();
            console.log(res, '本人销售额')
            this.setData({
                userData: res.data.ResData || {}
            })
        } catch (e) {
            console.log(e, '获取本人销售额失败')
        }
    },
})